/**
 * Created by scalpel on 2016/9/27.
 */



var api = frameElement.api, W = api.opener;
function initUp(container){
    var data=api.data;
    var upPath=data.upUrl;
    if (upPath==null||upPath==""){
        alert('没有上传地址，无法初始化上传控件');
        return;
    }

    if(upPath.indexOf("?")==-1){
        upPath=upPath+"?uploadFile=yes&AjaxMode=true";
    }else {
        upPath=upPath+"&uploadFile=yes&AjaxMode=true";
    }
    var uploader = new plupload.Uploader({
        runtimes : 'html5,flash,silverlight,html4',
        browse_button : 'pickfiles', // you can pass in id...
        container: document.getElementById(container), // ... or DOM Element itself
        chunk_size : '10mb',
        url :path+ upPath+"&i_="+Math.random(),
        filters : {
            max_file_size : '50mb',
            mime_types: [
                {title : "excel files", extensions : "xls,xlsx"}
            ]
        },
        flash_swf_url : '/plupload/js/Moxie.swf',
        silverlight_xap_url : '/plupload/js/Moxie.xap',
        init: {
            PostInit: function() {
                document.getElementById('filelist').innerHTML = '';
                document.getElementById('uploadfiles').onclick = function() {

                    uploader.start();
                    return false;
                };
            },
            BeforeUpload:function(uploader,file){
                $("#importErr").append("<span>"+file.name+">>开始上传>></span></br>");
            },
            FilesAdded: function(up, files) {
                plupload.each(files, function(file) {
                    $('#uploadfiles').addClass('active');

                    /**
                     * 显示选择了那些文件
                     *
                     *
                     **/
                    var fListtr="<tr id='"+file.id+"'>" +
                        "<td class=\"bd\">"+file.name+"</td>"+
                        "<td class=\"bd\">"+plupload.formatSize(file.size)+"</td>"+
                        "<td name=progress class=\"bd\"></td>"+
                        "<td name=result class=\"bd\"></td>";
                    $("#filelistTable").append(fListtr);
                    //uploader.start();
                    // document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
                });
            },

            UploadProgress: function(up, file) {
                //上传进度

                $("td[name=progress]","#"+file.id).html(file.percent+"%")
            },

            Error: function(up, err) {
                //上传错误信息
                var resp=err.response;
                var res= $.parseJSON(resp);
                $("#importErr").append("<p>"+res.error.message+"</p><br />");
                $("td[name=result]","#"+err.file.id).addClass("bd color-r").html("失败");
                $("*[name=importErr]").show();

            },
            UploadComplete:function(up,fils){
                upPath="";
                var i1=0;
                $("p","#importErr").each(function(i,d){
                    var htm=$(d).html();
                    var ii=getlen(htm,"<br");

                    i1=(parseInt(i1)+parseInt(ii));
                });
                $("#errNum").html(i1);
                if (i1>0){
                    $("#uploadSuccess").hide();
                }


            },
            FileUploaded: function(up, file, info) {
                var result = $.parseJSON(info.response);
                if(!result.bool) {
                    if(result.message === 'sessionStatusFalse') { //超时判断
                        var msg = result.result;
                        if(window.parent.timeoutMsg != undefined)
                        {
                            window.parent.timeoutMsg(msg);
                        }
                        return;
                    }
                }
                //上传成功
                $("td[name=result]","#"+file.id).addClass("bd color-g").html("成功");
                var fNum=$(".color-r","#"+file.id).length;
                if (fNum>0){
                    $("#uploadSuccess").hide();
                }else {
                    $("#uploadSuccess").show();
                }
                $("#importErr").append("<span>"+file.name+">>上传成功!</span></br>");

                if(window.parent.setFileName != undefined)
                {
                    window.parent.setFileName(file.name);
                }
            },
        }
    });

    uploader.init();
}